// pages/payPresent/payPresent.js
var app = getApp()
var util = require('../../utils/util.js')
var host = app.globalData.host
var self = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    presentId: '',
    count: 1,
    userName: null,
    postalCode: null,
    provinceName: null,
    cityName: null,
    countyName: null,
    detailInfo: null,
    nationalCode: null,
    telNumber: null,
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    numbers: [],
    total: 0
  },
  getStyle() {
    wx.setNavigationBarTitle({
      title: '购买'
    })
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  getNumbers() {
    var temp = []
    for (var i = 1; i <= 10; i++) {
      temp.push(i)
    }
    self.setData({
      numbers: temp
    })
  },
  address() {
    wx.chooseAddress({
      success: function (res) {
        self.setData({
          userName: res.userName,
          postalCode: res.postalCode,
          provinceName: res.provinceName,
          cityName: res.cityName,
          countyName: res.countyName,
          detailInfo: res.detailInfo,
          nationalCode: res.nationalCode,
          telNumber: res.telNumber,
        })
      }
    })
  },
  decrease() {
    var count = this.data.count
    if (count <= 1) {
      count = 1;
    } else {
      count -= 1;
    }
    this.setData({
      count: count
    })
    self.counter()
  },
  plus() {
    var count = this.data.count
    count += 1;
    this.setData({
      count: count
    })
    self.counter()
  },
  pickerChange(e) {
    var count = parseInt(e.detail.value)
    self.setData({
      count: count
    })
    self.counter()
  },
  counter() {
    var price = self.data.info.price.toFixed(2)
    var count = parseInt(self.data.count)
    var total = price * count
    self.setData({
      total: total
    })
  },
  getInfo() {
    app.request({
      url: host + '/xhc/present/info',
      data: {
        id: self.data.presentId
      },
      success: function (res) {
        self.setData({
          info: res.data.info
        })
        self.counter()
      }, fail: function (msg) {
        console.warn(msg)
      }
    })
  },
  onSubmit() {
    var url = host + '/xhc/present/pay'
    if (!util.validateData(app.globalData.openId)) {
      wx.showModal({
        title: '提示',
        content: '还没允许获取用户信息',
        confirmText: '去设置',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
              }
            })
          }
        }
      })
      return;
    }
    if (!util.validateData(self.data.userName) || !util.validateData(self.data.telNumber) || !util.validateData(self.data.provinceName)) {
      util.showModel('请填写收货地址')
      return;
    }
    wx.showLoading({
      title: '支付中',
    })
    app.request({
      url: url,
      data: {
        presentId: self.data.presentId,
        userName: self.data.userName,
        postalCode: self.data.postalCode,
        provinceName: self.data.provinceName,
        cityName: self.data.cityName,
        countyName: self.data.countyName,
        detailInfo: self.data.detailInfo,
        nationalCode: self.data.nationalCode,
        telNumber: self.data.telNumber,
        count: this.data.count
      }, success: function (res) {
        wx.hideLoading()
        var response = res.data.info
        var nonce_str = response.nonce_str
        var prepay_id = response.result.prepay_id
        var sign = response.sign
        var timestamp = response.timeStamp
        var appId = response.appid
        var param = {
          'appId': appId,
          'timeStamp': '' + timestamp,
          'nonceStr': '' + nonce_str,
          'package': 'prepay_id=' + prepay_id,
          'signType': 'MD5',
          'paySign': '' + sign,
          'success': function (res) {
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/success/success?isShowContent=' + 0
              })
            }, 200)
          },
          'fail': function (res) {
            util.showModel('支付失败')
          }
        }
        wx.requestPayment(param)
      }, fail: function (res) {
        wx.hideLoading()
        util.showModel(res.data.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this
    self.setData({
      presentId: options.presentId
    })
    self.getStyle()
    self.getNumbers()
    self.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})